package net.gylka.newsreader;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SiteListAdapter extends ArrayAdapter<NewsSite> {

	private ArrayList<NewsSite> mNewsSiteList;
	
	public SiteListAdapter(Context context, int textViewResourceId,	List<NewsSite> objects) {
		super(context, textViewResourceId, objects);
		mNewsSiteList = (ArrayList<NewsSite>) objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View itemView = convertView;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			itemView = inflater.inflate(R.layout.list_site_item, parent, false);
		}
		TextView txtNewsSiteName = (TextView) itemView.findViewById(R.id.txtNewsSiteName);
		txtNewsSiteName.setText(mNewsSiteList.get(position).mName);
		TextView txtNewsSiteUrl = (TextView) itemView.findViewById(R.id.txtNewsSiteUrl);
		txtNewsSiteUrl.setText(mNewsSiteList.get(position).mUrl);
		

		// TODO: Implement handling different news sources
		itemView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getContext(), ArticleActivity.class);
				intent.putExtra(BaseNewsReaderActivity.PACKAGE_NAME + ".SiteName", NewsSite.ID_PRAVDA_NEWS);
				v.getContext().startActivity(intent);
			}
			
		});
	
		return itemView;
	}


}
